package book

import (
	domainBook "BookStack/internal/domain/book"
	"BookStack/pkg/utils"
)

// =========================== 请求DTO ===========================

// CreateBookRequest 创建书籍请求
type CreateBookRequest struct {
	Title       string `json:"title" binding:"required"`
	Description string `json:"description"`
	Cover       string `json:"cover"`
	Slug        string `json:"slug"`
	CreatedBy   uint   `json:"created_by" binding:"required"`
}

// UpdateBookRequest 更新书籍请求
type UpdateBookRequest struct {
	Title       string `json:"title" binding:"required"`
	Description string `json:"description"`
	Cover       string `json:"cover"`
	Slug        string `json:"slug"`
}

// BookStatusRequest 书籍状态请求
type BookStatusRequest struct {
	Status int `json:"status" binding:"required,oneof=0 1 2"` // 0草稿 1发布 2禁用
}

// BookRecommendedRequest 书籍推荐状态请求
type BookRecommendedRequest struct {
	Recommended bool `json:"recommended" binding:"required"`
}

// BookQueryParams 书籍查询参数
type BookQueryParams struct {
	Page      int  `json:"page" form:"page"`
	PageSize  int  `json:"page_size" form:"page_size"`
	Status    *int `json:"status" form:"status"`
	Recommend *int `json:"recommend" form:"recommend"`
}

// CreateChapterRequest 创建章节请求
type CreateChapterRequest struct {
	BookID    uint   `json:"book_id" binding:"required"`
	Title     string `json:"title" binding:"required"`
	Content   string `json:"content"`
	Slug      string `json:"slug"`
	Sort      int    `json:"sort"`
	CreatedBy uint   `json:"created_by" binding:"required"`
}

// UpdateChapterRequest 更新章节请求
type UpdateChapterRequest struct {
	Title   string `json:"title" binding:"required"`
	Content string `json:"content"`
}

// ChapterReorderRequest 章节重排序请求
type ChapterReorderRequest struct {
	BookID        uint                  `json:"book_id" binding:"required"`
	ChapterOrders []ChapterOrderRequest `json:"chapter_orders" binding:"required"`
}

// ChapterOrderRequest 章节排序请求
type ChapterOrderRequest struct {
	ID   uint `json:"id" binding:"required"`
	Sort int  `json:"sort" binding:"required"`
}

// RecycleBinQueryParams 回收站查询参数
type RecycleBinQueryParams struct {
	Page     int    `json:"page" form:"page"`
	PageSize int    `json:"page_size" form:"page_size"`
	Type     string `json:"type" form:"type"`
}

// BatchRestoreRequest 批量恢复请求
type BatchRestoreRequest struct {
	IDs []uint `json:"ids" binding:"required"`
}

// PermanentDeleteRequest 永久删除请求
type PermanentDeleteRequest struct {
	IDs []uint `json:"ids" binding:"required"`
}

// RecycleBinItem 回收站项目
type RecycleBinItem struct {
	ID         uint           `json:"id"`
	EntityID   uint           `json:"entity_id"`
	EntityType string         `json:"entity_type"`
	Title      string         `json:"title"`
	DeletedAt  utils.JSONTime `json:"deleted_at"`
	DeletedBy  uint           `json:"deleted_by"`
	ParentID   *uint          `json:"parent_id,omitempty"`
	ParentName *string        `json:"parent_name,omitempty"`
}

// =========================== 响应DTO ===========================

// BookResponse 书籍响应
type BookResponse struct {
	ID           uint           `json:"id"`
	Title        string         `json:"title"`
	Description  string         `json:"description"`
	Cover        string         `json:"cover"`
	Slug         string         `json:"slug"`
	Status       int            `json:"status"`
	IsRecommend  bool           `json:"is_recommend"`
	CreatedBy    uint           `json:"created_by"`
	CreatedAt    utils.JSONTime `json:"created_at"`
	UpdatedAt    utils.JSONTime `json:"updated_at"`
	ViewCount    int64          `json:"view_count"`
	LikeCount    int64          `json:"like_count"`
	StarCount    int64          `json:"star_count"`
	ChapterCount int64          `json:"chapter_count"`
}

// BookListResponse 书籍列表响应
type BookListResponse struct {
	Total int64           `json:"total"`
	Items []*BookResponse `json:"items"`
}

// ChapterResponse 章节响应
type ChapterResponse struct {
	ID        uint           `json:"id"`
	BookID    uint           `json:"book_id"`
	Title     string         `json:"title"`
	Content   string         `json:"content"`
	Slug      string         `json:"slug"`
	Order     int            `json:"order"`
	CreatedBy uint           `json:"created_by"`
	CreatedAt utils.JSONTime `json:"created_at"`
	UpdatedAt utils.JSONTime `json:"updated_at"`
	ViewCount int64          `json:"view_count"`
}

// RecycleBinListResponse 回收站列表响应
type RecycleBinListResponse struct {
	Total int64             `json:"total"`
	Items []*RecycleBinItem `json:"items"`
}

// =========================== 转换函数 ===========================

// BookToResponse 将领域实体转换为响应DTO
func BookToResponse(book *domainBook.Book) *BookResponse {
	return &BookResponse{
		ID:           book.ID,
		Title:        book.Title,
		Description:  book.Description,
		Cover:        book.Cover,
		Slug:         book.Slug,
		Status:       book.Status,
		IsRecommend:  book.Recommended > 0,
		CreatedBy:    book.CreatedBy,
		CreatedAt:    utils.TimeToJSONTime(book.CreatedAt),
		UpdatedAt:    utils.TimeToJSONTime(book.UpdatedAt),
		ViewCount:    int64(book.ViewCount),
		LikeCount:    int64(book.LikeCount),
		StarCount:    int64(book.StarCount),
		ChapterCount: int64(book.ChapterCount),
	}
}

// BooksToResponseList 将书籍列表转换为响应DTO
func BooksToResponseList(books []*domainBook.Book, total int64) *BookListResponse {
	items := make([]*BookResponse, 0, len(books))
	for _, book := range books {
		items = append(items, BookToResponse(book))
	}

	return &BookListResponse{
		Total: total,
		Items: items,
	}
}

// ChapterToResponse 将章节实体转换为响应DTO
func ChapterToResponse(chapter *domainBook.Chapter) *ChapterResponse {
	return &ChapterResponse{
		ID:        chapter.ID,
		BookID:    chapter.BookID,
		Title:     chapter.Title,
		Content:   chapter.Content,
		Slug:      chapter.Slug,
		Order:     chapter.Order,
		CreatedBy: chapter.CreatedBy,
		CreatedAt: utils.TimeToJSONTime(chapter.CreatedAt),
		UpdatedAt: utils.TimeToJSONTime(chapter.UpdatedAt),
		ViewCount: int64(chapter.ViewCount),
	}
}
